import { createRouter, createWebHistory } from 'vue-router'

// 导入页面组件
import Layout from '@/layout/index.vue'
import Dashboard from '@/views/dashboard/index.vue'
import UserManagement from '@/views/user/index.vue'
import Settings from '@/views/settings/index.vue'
import Login from '@/views/login/index.vue'


// 业务系统相关页面 - uniapp风格命名
import HetonglclbPage from '@/views/business/hetonglclb/hetonglclb.vue'
import HetonglcxqPage from '@/views/business/hetonglcxq/hetonglcxq.vue'
import HetonglcxqEditPage from '@/views/business/hetonglcxq_edit/hetonglcxq_edit.vue'


// 统计报表

// 系统管理相关页面
import ClientHub from '@/views/system_management/ClientHub/ClientHub.vue'
import EquipMng from '@/views/system_management/EquipMng/EquipMng.vue'

const routes = [
  // {
  //   path: '/redirect',
  //   component: Layout,
  //   meta: { hideInMenu: true },
  //   children: [
  //     {
  //       path: '/redirect/:path(.*)',
  //       component: () => import('@/views/redirect/index.vue')
  //     }
  //   ]
  // },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录',
      hideInMenu: true
    }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: {
          title: '控制台',
          icon: 'el-icon-s-home',
          affix: true
        }
      }
    ]
  },
  // 业务系统路由 - uniapp风格命名
  {
    path: '/business',
    component: Layout,
    redirect: '/business/hetonglclb/hetonglclb',
    meta: {
      title: '业务系统',
      icon: 'el-icon-office-building',
      hideInMenu: true  // 隐藏静态菜单，使用动态菜单
    },
    children: [
      // 合同流程列表页 (对应 dynamic)
      {
        path: '/business/hetonglclb/hetonglclb',
        name: 'HetonglclbPage',
        component: HetonglclbPage,
        meta: {
          title: '合同流程列表',
          icon: 'el-icon-document-copy'
        }
      },
      // 合同流程详情页 (对应 detail)
      {
        path: '/business/hetonglcxq/hetonglcxq',
        name: 'HetonglcxqPage',
        component: HetonglcxqPage,
        meta: {
          title: '合同流程详情',
          icon: 'el-icon-view',
          hideInMenu: true
        }
      },
      // 合同流程编辑页 (对应 general)
      {
        path: '/business/hetonglcxq_edit/hetonglcxq_edit',
        name: 'HetonglcxqEditPage',
        component: HetonglcxqEditPage,
        meta: {
          title: '合同流程编辑',
          icon: 'el-icon-edit-outline',
          hideInMenu: true
        }
      }
    ]
  },
  // 动态路由 - 映射到 hetonglclb (列表页)
  {
    path: '/dynamic',
    component: Layout,
    meta: { hideInMenu: true },
    children: [
      {
        path: ':category/:item',
        name: 'DynamicPage',
        component: HetonglclbPage, // 使用 hetonglclb 作为动态列表页
        props: (route) => ({
          category: route.params.category,
          item: route.params.item
        }),
        meta: {
          title: '动态页面',
          icon: 'Document',
          activeMenu: '/dynamic',
          dynamicTitle: true,
          hideInMenu: true
        }
      }
    ]
  },
  // 系统管理路由
  {
    path: '/system_management',
    component: Layout,
    meta: {
      title: '系统管理',
      icon: 'el-icon-setting'
    },
    children: [
      {
        path: 'ClientHub',
        name: 'ClientHub',
        component: ClientHub,
        meta: {
          title: '客户管理',
          icon: 'el-icon-user'
        }
      },
      {
        path: 'EquipMng',
        name: 'EquipMng',
        component: EquipMng,
        meta: {
          title: '设备管理',
          icon: 'el-icon-suitcase'
        }
      },
      {
        path: 'list_page',
        name: 'ListPage',
        component: () => import('@/views/system_management/list_page/list_page.vue'),
        meta: {
          title: '列表页面',
          icon: 'el-icon-list'
        }
      }
    ]
  },
  // 将用户管理和系统设置隐藏
  {
    path: '/user',
    component: Layout,
    meta: {
      hideInMenu: true // 隐藏菜单项
    },
    children: [
      {
        path: '',
        name: 'UserManagement',
        component: UserManagement,
        meta: {
          title: '用户管理',
          icon: 'el-icon-user',
          hideInMenu: true
        }
      }
    ]
  },
  {
    path: '/settings',
    component: Layout,
    meta: {
      hideInMenu: true // 隐藏菜单项
    },
    children: [
      {
        path: '',
        name: 'Settings',
        component: Settings,
        meta: {
          title: '系统设置',
          icon: 'el-icon-setting',
          hideInMenu: true
        }
      }
    ]
  },

  // 统计报表路由
  {
    path: '/tongjibb',
    component: Layout,
    meta: { title: '统计报表', icon: 'el-icon-data-analysis', hideInMenu: true },
    children: [
      {
        path: '',
        name: 'TongjibbIndex',
        component: () => import('@/views/business/tongjibb/tongjibb.vue'),
        meta: { title: '统计报表', hideInMenu: true }
      },
      {
        path: 'tjtb',
        name: 'TongjibbTjtb',
        component: () => import('@/views/business/tongjibb/tjtb.vue'),
        props: true,
        meta: { title: '统计报表明细', hideInMenu: true }
      },
      {
        path: ':subPath',
        name: 'TongjibbSubPage',
        component: () => import('@/views/business/tongjibb/tongjibb.vue'),
        props: true,
        meta: { title: '统计报表明细', hideInMenu: true }
      }
    ]
  }
]

console.log('[路由配置] routes =', routes)

const router = createRouter({
  // 根据部署路径设置 base，与 vite.config.js 中的 base 保持一致
  history: createWebHistory('/hzshtgl/dist/'),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})


// 路由守卫
router.beforeEach((to, from, next) => {
  // 这里可以添加权限验证逻辑
  const token = localStorage.getItem('token')
  
  if (to.path === '/login') {
    next()
  } else if (!token) {
    next('/login')
  } else {
    next()
  }
})

export default router
